/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.util;

import java.awt.Window;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * An implementation of the AWTUtilities interface.  It tests to see
 * if the current JRE is Java 6 update 10 or higher.  If it is,
 * then it instantiates (using reflection)
 * and delegates to the J6u10AWTUtilities implementation.
 * 
 * 
 * @since Jun 19, 2009
 */

public class BasicAWTUtilities implements AWTUtilities
{
  
  private final Log logger = LogFactory.getLog(this.getClass());
  
  private final boolean translucencySupported;
  
  private final AWTUtilities delegate;
  
  public BasicAWTUtilities()
  {    
    if(isCompatibleJre())
    {
      logger.debug("Attempting to load the Java 6 Update 10 Translucency Suport Class.");
      //We might support translucency
      delegate = createJ6u10AWTUtilities();
      
      if(delegate != null)
      {
        translucencySupported = delegate.isTranslucencySupported();
      }
      else
      {
        translucencySupported = false;
      }
    }
    else
    {
      delegate = null;
      translucencySupported = false;      
    }

    if(translucencySupported)
    {
      logger.info("Translucency is supported.");
    }
    else
    {
      logger.info("Translucency is NOT supported.");      
    }
  }
  
  private boolean isCompatibleJre()
  {
    try
    {
      String jVersion = System.getProperty("java.version");
      String vendor = System.getProperty("java.vendor.url");
      logger.info("Java Version: " + jVersion + " " + vendor);

      String [] tokens = jVersion.split("[\\._]");
      int major = Integer.parseInt(tokens[0] + tokens[1]);
      // Update rev is the last token
      int updateRev = Integer.parseInt(tokens[tokens.length - 1]);
      
      logger.debug("Major: [" + major + "] Update Rev: [" + updateRev + "]");
      
      return vendor.equals("http://java.sun.com/") && major >= 16 && updateRev >= 10;
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
      logger.warn("Unable to determine JRE.  Translucency is disabled.", e);
    }
    catch (NumberFormatException e)
    {
      logger.warn("Unable to determine JRE.  Translucency is disabled.", e);
    }

    return false;      
  }
  
  private AWTUtilities createJ6u10AWTUtilities() {    
    ///TODO Find a way to load the class using Spring
    try
    {
      Class<?> clazz = Class.forName("org.tzconverter.util.J6u10AWTUtilities");
      return (AWTUtilities) clazz.newInstance();
    }
    catch (ClassNotFoundException e)
    {
      logger.warn("Unable to load translucent support class.  Translucent behavior is disabled.", e);
    }
    catch (InstantiationException e)
    {
      logger.warn("Unable to load translucent support class.  Translucent behavior is disabled.", e);
    }
    catch (IllegalAccessException e)
    {
      logger.warn("Unable to load translucent support class.  Translucent behavior is disabled.", e);
    }
    catch(UnsupportedClassVersionError e)
    {
      logger.warn("Unable to load translucent support class.  Translucent behavior is disabled.", e);      
    }
    
    return null;
  }
  
  /** Returns true if running on Java 6 update 10 or higher, and 
   * the current platform supports translucent heavy weight windows.
   * 
   * @see org.tzconverter.util.AWTUtilities#isTranslucencySupported()
   */
  @Override
  public boolean isTranslucencySupported()
  {    
    return translucencySupported;
  }

  /**
   * Sets the Window's opacity.  If the current JRE or platform doesn't support
   * translucency, then this method does nothing and does not throw an exception.
   * 
   * @see org.tzconverter.util.AWTUtilities#setWindowOpacity(java.awt.Window, float)
   */
  @Override
  public void setWindowOpacity(Window window, float opacity)
  {
    if(translucencySupported)
    {
      delegate.setWindowOpacity(window, opacity);
    }
  }

  /** Gets the Window's opacity.  If the current JRE or platform doesn't support
   * translucency, then this method always returns 1.0f.
   * 
   * @see org.tzconverter.util.AWTUtilities#getWindowOpacity(java.awt.Window)
   */
  @Override
  public float getWindowOpacity(Window window)
  {
    if(translucencySupported)
    {
      return delegate.getWindowOpacity(window);
    }
    return 1.0f;
  }
  
  
}
